package com.thb.user.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import com.thb.configure.config.Config;
import com.thb.user.dao.IUserCodeDAO;
import com.thb.user.model.UserCode;

@Repository
public class UserCodeDAOImpl implements IUserCodeDAO {
	
	@Autowired
	private JdbcTemplate jtemplate;
	
	@Autowired
	private Config config;


	public int updateUserCode(String uid, String usercode, byte type) {
		String sql =  "update usercode set usercode= ? where uid = ? and type = ? ";
		
		Object[] args = new Object[]{usercode,uid,type};
		int[] types =new int[]{Types.VARCHAR,Types.VARCHAR,Types.BIT};
		
		int id = jtemplate.update(sql.toString(), args,types);
		return id;
	}
	
	@Override
	public int updatePwd(int uid,String pwd) {
		String sql = "update usercode set passwd= ? where `uid` = ?";
		Object[] args = new Object[]{pwd,uid};
		int[] types = new int[]{Types.VARCHAR,Types.VARCHAR};
		int id = jtemplate.update(sql,args, types);
		return id;
	}

	
	public UserCode userCodeMapper(ResultSet rs, int no) throws SQLException{
		UserCode vo = new UserCode();
		vo.setUid(rs.getInt("uid"));
		vo.setPasswd(rs.getString("passwd"));
		vo.setUsercode(rs.getString("usercode"));
		vo.setType(rs.getByte("type"));
		
		return vo;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public UserCode findByUsercode(String usercode) {
		String sql = "select * from usercode where usercode = ?";
		Object[] args = new Object[]{usercode};
		List<UserCode> uList = jtemplate.query(sql, args, new RowMapper(){
			public Object mapRow(ResultSet rs, int no) throws SQLException {

				return userCodeMapper(rs, no);
			}
		});
		
		if(uList.size()==0){
			return null;
		}
		
		return uList.get(0);
		
	}

	@Override
	public int insert(final UserCode user) {
		KeyHolder keyHolder = new GeneratedKeyHolder();  
		final String sql = "insert into usercode(`usercode`,`passwd`,`type`) values (?,?,?)";
		jtemplate.update(new PreparedStatementCreator() {  
		        public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {  
		              
		            //String sql_sms = "insert into  sms(title,content,date_s,form,sffs,by1,by2,by3) values (?,?,'"+dates+"',?,?,?,?,?)";   
		               PreparedStatement ps = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);  
		               ps.setString(1, user.getUsercode());  
		               ps.setString(2, user.getPasswd());  
		               ps.setByte(3, user.getType());  
		               return ps;  
		        }  
		    }, keyHolder);  
		      
		    int generatedId = keyHolder.getKey().intValue();
		    return generatedId;
	}

	@Override
	public int findMaxId() {
		String sql = "select max(id) from usercode";
		int id =  jtemplate.queryForInt(sql);
		return id;
	}

	@SuppressWarnings({"unchecked","rawtypes"})
	@Override
	public UserCode findByUid(int uid) {
		String sql = "select * from usercode where uid = ?";
		Object[] args = new Object[]{uid};
		
		List<UserCode> uList = jtemplate.query(sql, args, new RowMapper(){
			public Object mapRow(ResultSet rs, int no) throws SQLException {

				return userCodeMapper(rs, no);
			}
		});
		
		if(uList.size()==0){
			return null;
		}
		
		return uList.get(0);
	}

}
